var a = require("/utils/util.js"), e = require("/utils/network.js"), t = require("/utils/md5.js"), o = require("/utils/url.js"), s = require("/utils/storage.js"), n = require("/utils/globalData.js"), r = require("/utils/role.js"), u = require("/utils/AES.js"), c = "", i = "", l = "";

App({
    onLaunch: function(a) {
        c = a.path, i = a.scene, a && a.query && (l = JSON.stringify(a.query));
        var e = this;
        e.globalData.accessToken = wx.getStorageSync(e.storage.accessToken), wx.checkSession({
            success: function() {
                e.log("未过期，并且在本生命周期一直有效");
                var a = wx.getStorageSync(e.storage.openid);
                a && a.length > 0 ? e.network.post({
                    url: e.url.openIdLogin,
                    params: {
                        openId: a,
                        path: c,
                        scene: i,
                        query: l
                    },
                    success: function(a) {
                        1 == a.data.result ? (wx.setStorageSync(e.storage.openid, a.data.data.openid), wx.setStorageSync(e.storage.accessToken, a.data.data.accessToken), 
                        wx.setStorageSync(e.storage.hunyu, a.data.data.hunyu), wx.setStorageSync(e.storage.role, a.data.data.role), 
                        wx.setStorageSync(e.storage.authToken, a.data.data.authToken), e.globalData.authToken = a.data.data.authToken, 
                        e.globalData.accessToken = a.data.data.accessToken, e.globalData.firstLogin = !1, 
                        e.util.lastAccessServer()) : 2 == a.data.result && (e.log("在其他地方登录"), e.util.doLoginWork());
                    }
                }) : (e.log("session已过期，需重新登录"), e.login());
            },
            fail: function() {
                e.login();
            },
            complete: function() {
                e.weixinProgramOpen(), e.util.getWPDownloadAppInfo();
            }
        });
    },
    login: function() {
        var a = this;
        a.log("调用登录"), wx.login({
            success: function(e) {
                e.code ? a.network.post({
                    url: a.url.weixinLogin,
                    params: {
                        code: e.code,
                        path: c,
                        scene: i,
                        query: l
                    },
                    success: function(e) {
                        1 == e.data.result ? (wx.setStorageSync(a.storage.openid, e.data.data.openid), wx.setStorageSync(a.storage.accessToken, e.data.data.accessToken), 
                        wx.setStorageSync(a.storage.role, e.data.data.role), wx.setStorageSync(a.storage.hunyu, e.data.data.hunyu), 
                        wx.setStorageSync(a.storage.authToken, e.data.data.authToken), a.globalData.accessToken = e.data.data.accessToken, 
                        a.globalData.authToken = e.data.data.authToken, a.globalData.firstLogin = !1, a.util.lastAccessServer()) : a.util.showToast(e.data.message);
                    }
                }) : a.log("获取用户登录态失败！" + e.errMsg);
            },
            fail: function(a) {}
        });
    },
    weixinProgramOpen: function() {
        var a = this;
        a.network.post({
            url: a.url.weixinProgramOpen,
            params: {
                path: c,
                scene: i,
                query: l
            },
            success: function(a) {}
        });
    },
    globalData: n,
    util: a,
    network: e,
    md5: t,
    AES: u,
    url: o,
    role: r,
    storage: s,
    debug: !0,
    log: function(a) {
        this.debug && console.log(a);
    }
});